<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>AK JOB</title>
    <link rel="icon" type="image/x-icon" href="../../../layuiadmin/style/res/favicon.png"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card layui-padding-3" id="applicationJobForm">
        <div class="layui-form" lay-filter="applicationJobForm">
            <fieldset class="layui-elem-field layui-field-title">
                <legend>基础配置</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label required">任务名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id" value="">
                    <input type="text" name="name" lay-verify="required|max100" placeholder="请输入任务名称"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">执行方法类</label>
                <div class="layui-input-block">
                    <input type="text" name="beanName" lay-verify="required|max100" placeholder="请输入执行方法类"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">cron表达式</label>
                <div class="layui-input-block">
                    <input id="cronStr" type="text" name="cronStr" lay-verify="required" placeholder="请输入cron表达式"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">路由策略</label>
                <div class="layui-input-block">
                    <select lay-filter="executiveStrategy" name="executiveStrategy" lay-verify="required">
                        <option value="">请选择</option>
                        <option value="1">第一个</option>
                        <option value="2">轮询</option>
                        <option value="3">最后一个</option>
                        <option value="4">随机</option>
                        <option value="5">最不经常使用</option>
                        <option value="6">最近最久未使用</option>
                        <option value="7">广播</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">自定义参数</label>
                <div class="layui-input-block">
                    <textarea name="paramsStr"
                              lay-verify="max100|isValidJson"
                              placeholder="请输入JSON格式参数"
                              lay-verType="tips"
                              class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">任务超时</label>
                    <div class="layui-input-block">
                        <div class="layui-input-group">
                            <input type="number" name="timeOut" lay-affix="number"
                                   autocomplete="off"
                                   lay-verType="tips"
                                   max="99999999"
                                   min="0"
                                   class="layui-input"/>
                            <div class="layui-input-split layui-input-suffix">
                                秒
                            </div>
                        </div>
                    </div>
                </div>
<!--                <div class="layui-inline">-->
<!--                    <label class="layui-form-label">失败重试</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <div class="layui-input-group">-->
<!--                            <input type="number" name="retryNum" lay-affix="number"-->
<!--                                   autocomplete="off"-->
<!--                                   lay-verType="tips"-->
<!--                                   min="0"-->
<!--                                   max="20"-->
<!--                                   class="layui-input"/>-->
<!--                            <div class="layui-input-split layui-input-suffix">-->
<!--                                次-->
<!--                            </div>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </div>-->
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">负责人</label>
                <div class="layui-input-block">
                    <input type="text" name="managerName" lay-verify="max100"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <fieldset class="layui-elem-field layui-field-title">
                <legend>异常通知</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label">报警邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="managerMailAddress" lay-verify="max100"
                           autocomplete="off"
                           lay-verType="tips"
                           class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">每小时最多</label>
                <div class="layui-input-block">
                    <div class="layui-input-group">
                        <input type="number" name="maxEmailsPerHour" lay-affix="number"
                               autocomplete="off"
                               lay-verType="tips"
                               min="0"
                               max="60"
                               class="layui-input"/>
                        <div class="layui-input-split layui-input-suffix">
                            次
                        </div>
                    </div>
                </div>
            </div>
            <input type="button" class="layui-btn layui-hide" style="width: 100%" lay-submit
                   lay-filter="applicationJobFormSubmit"
                   id="applicationJobFormSubmit" value="保存">
        </div>
    </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
        , cron: "cron/cron"
    }).use(['index', 'cron', 'auth', 'setter', 'AppHelper', 'applicationJob'], function () {
        var $ = layui.$
            , form = layui.form
            , setter = layui.setter
            , applicationJob = layui.applicationJob
            , AppHelper = layui.AppHelper
            , cron = layui.cron
            , LayerPage = AppHelper.getLayerPage()
        ;
        //解析当前路由信息
        var router = layui.router();
        var action = router.search.action;
        var id = router.search.id;

        form.verify({
            max100: function (value, elem) {
                if (value && value.length > 100) {
                    return '不能大于 100 个字符';
                }
            }, isValidJson: function (value, elem) {
                if (!value && value.length == 0){
                    return ;
                }
                try {
                    let parsed  = JSON.parse(value);
                    if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
                        return ;
                    }
                } catch (e) {
                    return '不是有效的JSON格式';
                }
                return '不是有效的JSON格式';
            }
        });
        var refreshParentTable = function () {
            parent.layui.table.reload("LAY-role-manage");
        }
        switch (action) {
            case "add":
                form.on("submit(applicationJobFormSubmit)", function (obj) {
                    var field = obj.field;
                    field.id = id
                    //保存
                    var layerPageLoad = LayerPage.load();
                    applicationJob.methods.add(field, 1)
                        .done(function (res) {
                            if (res.code == "200") {
                                layerPageLoad.close();
                                parent.layer.msg("新增成功！", {icon: 1}, function () {
                                })
                                refreshParentTable();
                                LayerPage.close();
                            }
                        })
                        .fail(function () {
                            layerPageLoad.close();
                        });
                })
                break;
            case "edit":
                $.when(applicationJob.methods.info(id))
                    .done(function (adminReq) {
                        var adminInfo = adminReq.data;
                        form.val("applicationJobForm", adminInfo)
                        cron.render({
                            elem: "#cronStr",                  // 绑定元素
                            run: setter.apiHost + "/admin/api/akApplicationJob/cron", // 获取最近运行时间的接口
                            done: function (cronStr) {       // 点击确定，触发，参数为 cron 表达式字符串
                                console.log(cronStr);
                            }
                        });
                        form.on("submit(applicationJobFormSubmit)", function (obj) {
                            var field = obj.field;
                            field.id = id
                            //保存
                            var layerPageLoad = LayerPage.load();
                            applicationJob.methods.update(field)
                                .done(function (res) {
                                    if (res.code == "200") {
                                        layerPageLoad.close();
                                        parent.layer.msg("修改成功！", {icon: 1}, function () {
                                        })
                                        refreshParentTable();
                                        LayerPage.close();
                                    }
                                })
                                .fail(function () {
                                    layerPageLoad.close();
                                });
                        })
                    })
                break;
        }


    });
</script>
</body>
</html>
